{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9e8a293d",
   "metadata": {},
   "source": [
    "# datetime.date 基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1c956aba",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from datetime import date, datetime as dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b1b9c1a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(9999, 12, 31)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date.max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e7f2be06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(1, 1, 1)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date.min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3da0ffd0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(1)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date.resolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3f60a10e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2022, 1, 4)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5efa4be7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2022, 1, 4)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date.fromtimestamp(time.time())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f529b064",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022 1 4\n"
     ]
    }
   ],
   "source": [
    "d = date.today()\n",
    "print(d.year, d.month, d.day)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "00e0f886",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2016, 1, 4)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.replace(2016)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "30f6bda0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2016, 3, 4)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.replace(2016, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "11ef7c1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2016, 3, 2)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.replace(2016, 3, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fb172280",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=1, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=4, tm_isdst=-1)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.timetuple()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9eaee1e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "738159"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.toordinal()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "270914c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.weekday()\n",
    "# 当周周一非工作日时，weekday() 与isoweekday()返回不一致，如2022-01-04"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "691427a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.isoweekday()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c9b5d201",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2022, 1, 2)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.isocalendar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8361d271",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2022-01-04'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.isoformat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f4df71de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tue Jan  4 00:00:00 2022'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.ctime()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab92defe",
   "metadata": {},
   "source": [
    "date 转 str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "83dd6949",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2022/01/04'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.strftime('%Y/%#m/%#d')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efbe7bb1",
   "metadata": {},
   "source": [
    "str 转 date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9fd39938",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2018, 8, 5)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str = \"20180805\"\n",
    "str2date = dt.strptime(str,\"%Y%m%d\")  #字符串转化为date形式\n",
    "str2date.date()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d44b1659",
   "metadata": {},
   "source": [
    "python格式化日期时间自动补0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "82552712",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022/02/15:16\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "# 年4位，月2位，日2位，时2位\n",
    "print(\"{0}/{1:0>2d}/{2:0>2d}:{3:0>2d}\".format(*time.localtime()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "af6a110c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=2, tm_mday=15, tm_hour=16, tm_min=42, tm_sec=1, tm_wday=1, tm_yday=46, tm_isdst=0)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64d0d6cf",
   "metadata": {},
   "source": [
    "格式化日期去掉前导的0 \"%Y%-m%-d\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e03d1658",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([         nan,          nan,          nan,          nan,\n",
       "                nan,          nan,          nan,          nan,\n",
       "                nan, 118.09211   , 118.05032   , 118.00923   ,\n",
       "       117.98761   , 117.96455   , 117.91103   , 117.8667    ,\n",
       "       117.83336   , 117.87194   , 117.95373   , 118.13439   ,\n",
       "       118.34183406])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import talib\n",
    "import numpy as np\n",
    "l = [117.9251, 118.0787, 118.1868, 118.2039, 118.39200000000001, 118.4103, 118.4169, 117.9683, 117.8932, 117.4459, 117.5072, 117.6678, 117.9706, 117.9733, 117.8568, 117.96700000000001, 118.0835, 118.3541, 118.7111, 119.2525, 119.58164055711731]\n",
    "\n",
    "talib.MA(np.array(l) , 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1477b7fb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
